package com.yloveg.quartz;

import java.math.BigDecimal;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.yloveg.entity.VActivity;
import com.yloveg.entity.VPayOrder;
import com.yloveg.entity.VPlayer;
import com.yloveg.service.VActivityService;
import com.yloveg.service.VPayOrderService;
import com.yloveg.service.VPayService;
import com.yloveg.service.VPlayerService;

@Component
public class ActivityQuartz {
	
	@Autowired
    private VActivityService vActivityService;
	
	@Autowired
    private VPlayerService vPlayerService;
	
    @Autowired
    private VPayOrderService vPayOrderService;
	
	//活动日访问量每天凌晨1点清零(活动中的活动)
	@Scheduled(cron="0 0 1 * * ?")
	public void work(){
		List<VActivity> list = vActivityService.queryActivityOpen();
		for(VActivity va : list) {
			VActivity van = new VActivity();
			van.setDaynum(0);
			van.setMoneyDay(new BigDecimal("0"));
			van.setId(va.getId());
			vActivityService.updateByPrimaryKeySelective(van);
		}
	}
	
	//每隔5分钟设置活动中得flag为N 
	//@Scheduled(cron="0 */5 * * * ?")
	/*public void setFlag(){
		List<VActivity> list = vActivityService.queryActivityOpen();
		for(VActivity va : list) {
			VActivity van = new VActivity();
			van.setId(va.getId());
			van.setFlag("N");
			vActivityService.updateByPrimaryKeySelective(van);
		}
	}*/
	
	//每隔2分钟统计下活动选手报名数
	@Scheduled(cron="0 */3 * * * ?")
	public void setPlaynum(){
		List<VActivity> list = vActivityService.queryActivity();
		for(VActivity va : list) {
			List<VPlayer> plist = vPlayerService.queryPlayerAll(va.getId());
			VActivity van = new VActivity();
			van.setId(va.getId());
			van.setPalyernum(plist.size());
			vActivityService.updateByPrimaryKeySelective(van);
		}
	}
	
	//每隔10分钟统计下活动选手报名数
	@Scheduled(cron="0 */10 * * * ?")
	public void countMoney(){
		List<VActivity> list = vActivityService.queryActivity();
		VPayOrder order = new VPayOrder();
		for(VActivity va : list) {
			order.setActivityid(String.valueOf(va.getId()));
			VActivity van = new VActivity();
			van.setId(va.getId());
			van.setMoneySum(BigDecimal.valueOf(vPayOrderService.selectSum(order)));
			van.setMoneyDay(BigDecimal.valueOf(vPayOrderService.selectByTodaySum(order)));
			vActivityService.updateByPrimaryKeySelective(van);
		}
	}
}
